#coding: utf-8

from django.shortcuts import render
from django.http import HttpResponse
from cmdb.models import *
from cmdb.views import *
from django.db import connection,transaction,connections
from django.http import JsonResponse
from django.core.exceptions import ObjectDoesNotExist
import hashlib
from django.http import HttpResponseRedirect
from diy_decorator.auth import *
import urllib
import urllib2
import json
from mysite.settings import jumpserver_url


#用户列表
@login_required()
def index(request):
    page_num=int(request.GET.get('page_num',pageNum))
    page=int(request.GET.get('page',1))
    count=EquipmentInfo.objects.count()
    page_total = int(count/page_num)
    if (count%page_num) > 0:
        page_total += 1
    startpage=(page-1)*page_num
    endpage=startpage+page_num
    # userRes = Radcheck.objects.order_by('-id')[startpage:endpage]
    userRes = {}
    context = {
        'user_list' : userRes,
        'page_total' : page_total,
        'page_list' : range(1,page_total+1),
        'page' : page,
        'page_num' : page_num,
        'count' : count
    }
    return render(request, 'cmdb/muser/user.html', context)

#其他验证操作
def passcheck(request):

    response_dict = {"status":1}
    password = request.POST.get('password','')
    username = request.POST.get('username','')
    sha = hashlib.sha1(password)
    password = sha.hexdigest()
    
    try:
        if username == 'Admin':
            userInfo = UserInfo.objects.get(username=username)
            if userInfo.password == password:
                request.session['username'] = username
                request.session['fullname'] = username
            else:
                response_dict['status'] = 0
                response_dict['message'] = '输入密码错误'
        else:
            test_data = {'username':username, 'action': 'get_user_info'}
            test_data_urlencode = urllib.urlencode(test_data)
            requrl = jumpserver_url + "user_action.php"
            req = urllib2.Request(url = requrl,data =test_data_urlencode)
            userInfo = urllib2.urlopen(req).read()
            userArr = json.loads(userInfo)
            if userArr and userArr['status'] == 1:
                jumpPassword = userArr['data']['value']
                fullname = userArr['data']['fullname']
                if password == jumpPassword:
                    request.session['username'] = username
                    request.session['fullname'] = fullname
                else:
                    response_dict['status'] = 0
                    response_dict['message'] = '输入密码错误'
            else:
                response_dict['status'] = 0
                response_dict['message'] = '用户不存在'
    except Exception,ex:
        print Exception,ex
        response_dict['status'] = 0
        response_dict['message'] = '服务出错，请联系管理员'

    response=JsonResponse(response_dict,safe=False)

    response["CHARSET"]='UTF-8'
    return response

#logout
@login_required()
def logout(request):
    try:
        del request.session['username']
    except KeyError:
        print KeyError
    # del request.session['username']
    return HttpResponseRedirect('/')

